<template>
	<view class="main-content">
		<div class="form bg-white">
			<div class="cover-display-area">
				<u-upload :action="action" ref="cover" :file-list="form.cover" :max-count="coverNumber" :multiple="false" width="160" height="160" class="upload-button-display-site"></u-upload>
			</div>
			<u-input :border="false" placeholder="请输入发表内容" v-model="form.content" type="textarea" class="content"></u-input>
		</div>
		<u-button plain shape="circle" @click="submitData" :custom-style="customStyle">保存</u-button>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	const app = getApp();
	import {
		edit,
		editInformation
	} from '@/api/circleOfFriends';
	export default {
		data() {
			return {
				coverNumber: 9,
				action: 'https://www.leyuanyinyong.cn/api/file/formimage',
				form: {
					cover: [],
					status: false
				},
				customStyle: {
					background: 'linear-gradient(-26deg, #20E6DF 0%, #00C9DB 72%)',
					position:'absolute',
					width:'90%',
					left:'5%',
					bottom:'20px',
					color: '#fff'
				},
			};
		},
		onLoad(option) {
			var _this = this;
			editInformation({
				id:option.id
			}).then(res => {
				if (res.code == 1) {
					_this.form = res.data;
					_this.form.cover = res.data.cover_images
				}
			})
		},
		methods: {
			insertImage() {
				var _this = this;
				uni.chooseImage({
					count: 1,
					success: (res) => {
						uni.uploadFile({
							url: _this.action, //仅为示例，非真实的接口地址
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (uploadFileRes) => {
								var data = JSON.parse(uploadFileRes.data);
								if (data.code == 0) {
									_this.$toast({
										title: '图片上传异常～'
									})
									return false;
								}
								_this.editorCtx.insertImage({
									src: data.data.url,
									alt: data.data.name
								})
							}
						});
					}
				})
			},
			submitData() {
				var _this = this;
				
				var coverImages = [];
				for (var key in _this.$refs.cover.lists) {
					coverImages.push({
						url: _this.$refs.cover.lists[key].response.data.url
					})
				}
				_this.form.cover_images = coverImages
				if (_this.form.content == undefined || _this.form.content.length <= 0) {
					_this.$refs.uToast.show({
						title: '内容不能为空'
					})
					return false;
				}
				edit(_this.form).then(res => {
					if (res.code == 1) {
						_this.$toast(res.msg);
						uni.navigateTo({
							url: '/pages/merchant/extension/list'
						});
					} else {
						_this.$refs.uToast.show({
							title: res.msg
						})
					}
				})
			}
		}
	};
</script>
<style lang="scss">
	.main-content{
		padding-top: 40rpx;
		.form{
			width: 90%;
			border-radius: 20rpx;
			margin: 0 auto;
			
			.cover-display-area{
				padding: 40rpx;
			}
			.content{
				padding: 0 40rpx !important;
			}
		}
	}
</style>
